<template>
  <el-dialog :visible.sync="visible" width="500px" custom-class="default-dialog"
    :close-on-click-modal="false" @close="cancel" append-to-body>
    <div slot="title">
      <img v-if="type" :src="require(`@/assets/img/${type}.png`)" alt="">
      <span>{{ title }} </span>
    </div>
    <el-form :model="form" :rules="rules" ref="form" label-width="80px" class="dialog-form" :disabled="type == 'view'">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="字典名称" prop="dictName">
            <el-input size="small" v-model="form.dictName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="描述" prop="remark">
            <el-input size="small" type="textarea" :maxlength="512" :rows="3" v-model="form.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button size="small" @click="cancel">取 消</el-button>
      <el-button size="small" v-if="type != 'view'" type="primary" @click="sure">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  data() {
    return {
      visible: false,
      title: '',
      form: {},
      rules: {
        dictName: [{ required: true, message: '请输入字典名称', trigger: 'blur' }]
      },
      type: ''
    };
  },
  methods: {
    showModal(type, row) {
      this.visible = true
      this.type = type
      if (type == 'add') {
        this.title = '新增'
        this.form = {
          "dictName": "",
          "remark": "",
        }
      } else if (type == 'edit') {
        this.title = '编辑'
        this.getInfoInit(row.sysDictId)
      } else if (type == 'view') {
        this.title = '详情'
        this.getInfoInit(row.sysDictId)
      }
    },
    // 详情
    getInfoInit(id){
      this.$api.getDictInfo({id:id}).then(res => {
        if(res && res.code == 200){
          let data = res.data || {}
          this.form = data
        }
      })
    },
    // 确定
    sure() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if(this.type == 'add'){
            this.$api.addDict(this.form).then(res => {
              if(res && res.code == 200){
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          }else if(this.type == 'edit'){
            this.$api.updateDict(this.form).then(res => {
              if(res && res.code == 200){
                this.$notify.success({
                  title: '成功',
                  message: '数据保存成功'
                });
                this.visible = false
                this.$emit('listInit')
              }
            })
          }
        }
      });
    },
    // 取消
    cancel() {
      this.visible = false;
      this.$refs["form"].resetFields();
    },
  }
};
</script>
